/**
 * 
 */
package fr.controller.actions;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.classic.Validatable;

import com.opensymphony.xwork2.ActionSupport;

import fr.beans.User;
import fr.model.dao.Hib.IDaoUserHB;

/**
 * @author Altenide-mov
 * Class d'action gérant le user.
 */
public class UserAction extends ActionSupport {

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 5308071143019543809L;


	/**
	 * The logger.
	 */
	private static final  Logger LOG 
	= Logger.getLogger(UserAction.class);

	// ================  Déclaration des services Dao =============== //

	/**
	 * Interface Dao for user.
	 */
	private IDaoUserHB iDaoUserHB;

	/**
	 * Constructor.
	 * @param piDaoUserHB
	 * 	the piDaoUserHB.
	 */
	public UserAction(final IDaoUserHB piDaoUserHB) {
		super();
		this.iDaoUserHB = piDaoUserHB;
	}


	// ===== attributes ====== //

	/**
	 * The User.
	 */
	private User user;


	/**
	 * The list of users.
	 */
	private List<User> listUser;

	// ======================= //	
	//    getters and setters  //
	// ======================= //	



	/**
	 * @return the user
	 */
	public final User getUser() {
		return user;
	}


	/**
	 * @param puser the user to set
	 */
	public final void setUser(final User puser) {
		this.user = puser;
	}

	/**
	 * @return the listUser
	 */
	public final List<User> getListUser() {
		return listUser;
	}


	/**
	 * @param plistUser the listUser to set
	 */
	public final void setListUser(final List<User> plistUser) {
		this.listUser = plistUser;
	}



	// ======================= //	
	// currents struts methods //
	// ======================= //

	/**
	 * Enregistrement d'un utilisateur.
	 * @return SUCCESS
	 */
	public final String saveUser() {

		LOG.info("Entering in registerUser" + user);

		if (this.iDaoUserHB.existUser(user)) {	
			
			addFieldError("user.login",
					"Login already Exist in base !, please change it");
			addActionError("Error saving user");
			return INPUT;
			
		} else if (this.iDaoUserHB.save(user) != 1) {
			
			LOG.info("Exit registerUser with success ! ");
			addActionMessage("Success saving User");
			return SUCCESS;
		}
		
		addActionError("Error saving user due to violation constraints");
		return ERROR;

	}


	/**
	 * @return SUCCESS.
	 */
	public final String showUsersList() {

		LOG.info("Entering in showUsersList");
		listUser = this.iDaoUserHB.showUsersList();
		LOG.info("List users size : " + listUser.size());
		LOG.info("Exit showUsersList with success ! ");		
		return SUCCESS;

	}


	/**
	 * @return SUCCESS.
	 */
	public final String readUserById() {

		LOG.info("Entering in readUser with user id : " + user.getId());
		user = this.iDaoUserHB.readUser(user.getId());
		LOG.info("Exit readUser with success ! user name is" + user.getName());	

		return SUCCESS;	

	}



}
